﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using BneyBaruch.Ingest.Core.ServiceProcess.Diagnostics;
using BneyBaruch.Ingest.MorningLesson.WorkflowProcessor.Services;
using System.ServiceModel;
using BneyBaruch.Ingest.Core.WorkflowCommunication;

namespace BneyBaruch.Ingest.MorningLessonWorkflowProcessorService
{
    public partial class MorningLessonWorkflowProcessorController : DebuggableService
    {
        #region Fields

        private WorkflowServiceHostBase morningLessonWorkflowProcessorServiceHost = null;
        private MorningLessonWorkflowProcessor morningLessonWorkflowProcessor = null;

        #endregion

        #region Constructors

        public MorningLessonWorkflowProcessorController()
        {
            InitializeComponent();
        }

        #endregion

        #region Start and Stop Overrides

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            StartServicesHosts();
        }

        protected override void OnStop()
        {
            base.OnStop();
            StopServicesHosts();
        }

        #endregion

        #region Services Hosts Manipulation Methods

        private void StopServicesHosts()
        {
            if (morningLessonWorkflowProcessorServiceHost.State == CommunicationState.Opened)
            {
                morningLessonWorkflowProcessorServiceHost.Close();
            }
            if (morningLessonWorkflowProcessor != null)
                morningLessonWorkflowProcessor.Dispose();
        }

        private void StartServicesHosts()
        {
            morningLessonWorkflowProcessor = new MorningLessonWorkflowProcessor();
            morningLessonWorkflowProcessorServiceHost = new WorkflowServiceHostBase(morningLessonWorkflowProcessor);
            morningLessonWorkflowProcessorServiceHost.Open();
        }

        #endregion
    }
}
